var express = require('express');
var router = express.Router();
var { cateListModel, MvListModel, Comment } = require('../model/model')
/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});



// --------------------------------刘廉洁
router.get('/getCateList', async (req, res) => {
  let num = req.query.cid
  // console.log(num);
  let list
  if (num == 0) {
    // console.log('全部');

    list = await cateListModel.find()
  } else {
    // console.log('单条');

    list = await cateListModel.find({ cid: num })
  }
  // console.log(list);

  res.send({
    code: 200,
    list
  })
})

router.get('/getMvList', async (req, res) => {
  let num = req.query.cid
  let list = await MvListModel.find({ cid: num })
  console.log(list);

  res.send({
    list: list[0]
  })
})

router.get('/getMvDetail', async (req, res) => {
  let { id, page } = req.query;
  let list = await MvListModel.find({ cid: id });

  // 检查查询结果是否为空
  if (list.length === 0) {
    return res.send({ message: 'No data found' });
  }

  // 获取第一个匹配的记录
  list = list[0];

  // console.log(list.classList,'3345');

  // 检查 classList 是否存在且非空
  if (!list.classList || list.classList.length === 0) {
    return res.send({ message: 'classList is empty' });
  }

  // 切片操作，如果需要分页
  const classList = list.classList.slice((page - 1) * 5, page * 5);


  res.send({
    list: list,
    classList: classList
  });
});

router.get('/getSongUrl',async (req, res) => {
  const {id, name} = req.query
  let list = await MvListModel.find({cid: id})
  list = list[0].classList.find(item => item.name === name)
  // console.log(list.link);
  res.send({
    list: list.link
  });
})

router.get('/updateState', async (req, res) => {
  const {id, name} = req.query
  console.log(id);
  
  let list = await MvListModel.find({cid: id})
  // console.log(list, 'list1');
  list = list[0].classList
  let obj = {}
  list.forEach(t=>{
    if(t.name === name){
      t.status = true
      obj = t
    }
  })
  // console.log(list, 'list2');
  
  await MvListModel.updateOne({cid: id}, {classList: list})
  res.send({
    code: 200,
    obj
  });
})

router.get('/updateStateN', async (req, res) => {
  const {id, name} = req.query
  console.log(id);
  
  let list = await MvListModel.find({cid: id})
  // console.log(list, 'list1');
  list = list[0].classList
  let obj = {}
  list.forEach(t=>{
    if(t.name === name){
      t.status = false
      obj = t
    }
  })
  // console.log(list, 'list2');
  
  await MvListModel.updateOne({cid: id}, {classList: list})
  res.send({
    obj,
    code: 200
  });
})




// 获取所有评论的接口
router.get('/comments', async (req, res) => {
  try {
    const comments = await Comment.find().populate('parent').exec(); // 使用 populate 获取父评论详情
    res.json(comments);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
});

// 提交新评论的接口
router.post('/comments', async (req, res) => {
  const { name, content, parent } = req.body;
  const newComment = new Comment({ name, content, parent });

  try {
    await newComment.save();
    res.status(201).json(newComment);
  } catch (error) {
    res.status(400).json({ message: error.message });
  }
});



module.exports = router;
